xlv.obj.frame={
	extend: function(){
		this.extend('xlv.obj.pane');
	},
	implement:function(){
		this.implement('xlv.imp.dnd');
	},
	pub:{
		icon:'/xlive/images/_frameIcon.png',
		exit:'/xlive/images/_frameExit.gif',
		min:'/xlive/images/_frameMin.gif',
		max:'/xlive/images/_frameMax.gif',
		sizer:'/xlive/images/_frameSizer.gif'
	},
	create:function(tag){
		var div=document.createElement((tag||'div'));
		div.setAttribute('xlvobject','xlv.obj.frame');
		Element.addClassName(div,'class_frame');
		return div;
	},
	properties:{
		glow:{enable:true},
		border:{enable:true,style:'top'},
		title:'untitle',
		iconURL:null,
		dnd:{enable:true,outline:true},
		exitable:true,
		iconable:true,
		maximumable:true,
		exclusive:false,
		modal:false,
		left:null,
		top:null,
		width:null,
		height:null
	},
	method:{
		construct:function(){
			if(Element.hasClassName(this,'modalglass'))return;
			this.$super('xlv.obj.pane.construct');
			var v=this.$V;
			Element.addClassName(this,'class_frame');
			Element.setStyle(v.root,{position:'relative',width:'100%',height:'100%',overflow:'hidden'});
			v.content=xlv.util.findChild(v.root,{id:'xlvid_frame_content'});
			if(!v.content){
				v.content=document.createElement('div');
				v.content.setAttribute('id','xlvid_frame_content');
				while(v.root.firstChild)v.content.appendChild(v.root.firstChild);
				v.root.appendChild(v.content);
			}
			v.content.style.backgroundColor='white';
			var icon,title;
			v.header=xlv.util.findChild(v.root,{id:'xlvid_frame_header'});
			if(!v.header){
				v.header=document.createElement('div');
				v.header.setAttribute('id','xlvid_frame_header');
				v.content.parentNode.insertBefore(v.header,v.content);
				v.header.appendChild(icon=document.createElement('img'));
				icon.setAttribute('id','xlvid_icon');
				icon.setAttribute('src',(this.$P.iconURL)?this.$P.iconURL:xlv.obj.frame.pub.icon);
				v.header.appendChild(v.exit=document.createElement('img'));
				v.exit.setAttribute('id','xlvid_exit');
				v.exit.setAttribute('src',xlv.obj.frame.pub.exit);
				v.header.appendChild(v.max=document.createElement('img'));
				v.max.setAttribute('id','xlvid_max');
				v.max.setAttribute('src',xlv.obj.frame.pub.max);
				v.header.appendChild(v.min=document.createElement('img'));
				v.min.setAttribute('id','xlvid_min');
				v.min.setAttribute('src',xlv.obj.frame.pub.min);
				v.header.appendChild(title=document.createElement('span'));
				title.setAttribute('id','xlvid_title');
				title.setAttribute('unselectable','on');
			}else{
				icon=xlv.util.findChild(v.header,{id:'xlvid_icon'});
				v.exit=xlv.util.findChild(v.header,{id:'xlvid_exit'});
				v.max=xlv.util.findChild(v.header,{id:'xlvid_max'});
				v.min=xlv.util.findChild(v.header,{id:'xlvid_min'});
				title=xlv.util.findChild(v.header,{id:'xlvid_title'});
			}
			v.footer=xlv.util.findChild(v.root,{id:'xlvid_frame_footer'});
			var sizer,note;
			if(!v.footer){
				v.footer=document.createElement('div');
				v.footer.setAttribute('id','xlvid_frame_footer');
				v.content.parentNode.appendChild(v.footer,v.content);
				v.footer.appendChild(sizer=document.createElement('img'));
				sizer.setAttribute('id','xlvid_sizer');
				sizer.setAttribute('src',xlv.obj.frame.pub.sizer);
				v.footer.appendChild(note=document.createElement('span'));
				note.setAttribute('id','xlvid_note');
			}
			this.styleForId(v.header,v.header.id);
			if(icon)this.styleForId(icon,icon.id);
			if(v.exit)this.styleForId(v.exit,v.exit.id);
			if(v.max)this.styleForId(v.max,v.max.id);
			if(v.min)this.styleForId(v.min,v.min.id);
			if(title)this.styleForId(title,title.id);
			this.styleForId(v.content,v.content.id);
			this.styleForId(v.footer,v.footer.id);
			if(sizer)this.styleForId(sizer,sizer.id);
			if(note)this.styleForId(note,note.id);
		},
		initial:function(){
			var v=this.$V;
			if(Element.hasClassName(this,'modalglass'))return;
			this.$super('xlv.obj.pane.initial');
			this.addEventHandler('mousedown','_mousedown');
			if(v.header)this.addEventHandler('dblclick','_dblclick',v.header);
			this.dnd_properties.dragOnly=true;
			this.dnd_properties.ghost=false;
			if(v.footer) {
				this.addEventHandler('mousemove','_footerMouseMove',v.footer);
				this.addEventHandler('mouseout','_footerMouseOut',v.footer);
			}
			v.modal=this.$P.modal;
			v.exclusive=this.$P.exclusive;
			v.footerEffecting=false;
			v.footerVisible=false;
			this.setTitle(this.$P.title);
			this.setLocation(this.$P.left,this.$P.top);
			this.setSize(this.$P.width,this.$P.height);
			this.setExitable(this.$P.exitable);
			this.setIconable((this.$P.modal) ? false:this.$P.iconable);
			this.setMaximumable(this.$P.maximumable);
			this.enableDnD(this.$P.dnd.enable);
			this.layout();
		},
		enableDnD:function(enable){
			this.dnd_enabled(enable);
		},
		styleForId:function(facilities,id){
			facilities.setAttribute('unselectable','on');
		},
		dispose:function(){
			var ret=xlv.sys.disposable(this);
			if(ret && ret.length >0) alert('cannot exit');
			else this.getDesktop().disposeFrame(this);
		},
		layout:function(){
			var v=this.$V;
			var h=this.offsetHeight-((v.header)?v.header.offsetHeight:0);
			if(v.content){
				if(Prototype.Browser.IE)Element.setStyle(v.content,{display:'none'});
				Element.setStyle(v.content,{display:'block',width:'auto',height:(h>0)? h+'px':'auto'});
			}
		},
		_heightPadding:function(){
			var padding=0;
			if(this.hasGlow()){
				var g1=this.targetElement(this.$V.root,{id:'xlvid_glow_g1'});
				var top=parseInt(Element.getStyle(g1,'paddingTop'));
				padding+=(!top)?0:top;
			}
			if(this.hasBorder()){
				if(this.$V.borderOption.style=='all')padding+=10;
				else padding +=6;
			}
			if(Prototype.Browser.IE)padding+=3;
			return padding;
		},
		setLocation:function(left,top){
			if(left!=null&&top!=null)Element.setStyle(this,{'left':left+'px','top':top+'px'});
		},	
		setSize:function(width,height){
			if(width!=null&&height!=null){
				Element.setStyle(this,{'width':width+'px','height':height+'px'});
				this.layout();
			}
		},
		setUrl:function(url){
			this.$V.url=url;//for reference
		},
		getUrl:function(){
			return this.$V.url;
		},
		setExitable:function(able){
			if(this.$V.exit)this.$V.exit.style.display=(able)?'':'none';
		},
		setIconable:function(able){
			if(this.$V.min)this.$V.min.style.display=(able)?'':'none';
		},
		setMaximumable:function(able){
			if(this.$V.max)this.$V.max.style.display=(able)?'':'none';
		},
		isActivated:function(){
			return this.hasClassName('activated');
		},
		isModal:function(){
			return (typeof this.$V.modal == 'undefined') ? this.$P.modal:this.$V.modal;
		},
		isExclusive:function(){
			return (typeof this.$V.exclusive == 'undefined') ? this.$P.exclusive:this.$V.exclusive;
		},
		_showFooter:function(visible){
			this.$V.footerEffecting=true;
			var options={element:this.$V.footer,minPx:2};
			if(visible){
				options.style={bottom:'0px'};
				options.finished=(function(){this.$V.footerEffecting=false;}).bind(this);
				xlv.util.styleEffect(options);
				this.$V.footerVisible=true;
			}else{
				this.$V.footerVisible=false;
				options.style={bottom:(2-this.$V.footer.offsetHeight)+'px'};
				options.finished=(function(){this.$V.footerEffecting=false;}).bind(this);
				xlv.util.styleEffect(options);
			}
		},
		_footerMouseMove:function(event){
			if(!this.hasClassName('activated')||this.$V.footerEffecting || this.$V.footerVisible||this.hasClassName('maximized')) return;
			var target=this.targetElement(event.target,{id:'xlvid_frame_footer'});
			if(target==this.$V.footer) 	this._showFooter(true);
		},
		_footerMouseOut:function(event){
			if(!this.hasClassName('activated')||this.$V.footerEffecting||!this.$V.footerVisible) return;
			var to=(event.toElement||event.relatedTarget);
			if((to && !xlv.util.isDescendantOf(to,this.$V.footer)))this._showFooter(false);
		},
		iconify:function(){
			this.getDesktop().iconifyFrame(this);
		},
		restore:function(){
			this.getDesktop().restoreFrame(this);
		},
		maximize:function(){
			this.getDesktop().maximumFrame(this);
		},
		_mousedown:function(event){
			if(this!=this.targetElement(event.target,{xlvobject:'xlv.obj.frame'})) return;
			this.getDesktop().activeFrame(this);
			switch(event.target.id){
				case 'xlvid_min' :
					event.stop();
					if(this.hasClassName('maximized')) this.restore();
					else this.iconify();
					break;
				case 'xlvid_max' :
					event.stop();
					if(this.hasClassName('maximized')) this.restore();
					else this.maximize();
					break;
				case 'xlvid_exit' :
					event.stop();
					this.enableDnD(false);
					this.dispose();
					break;
			}
		},
		_dblclick:function(event){
			if(this!=this.targetElement(event.target,{xlvobject:'xlv.obj.frame'})) return;
			var h=this.targetElement(event.target,{id:'xlvid_frame_header'});
			if(h && xlv.util.isDescendantOf(h,this)){
				event.stop();
				if(this.hasClassName('maximized')) this.restore();
				else this.maximize();
				return;
			}
		},
		/*override imp dnd*/
		dnd_enabled:function(enable){
			if(enable)this.addEventHandler('mousedown', '_dnd_chkmousedown',this.$V.heaader);
			else this.removeObserve('mousedown', '_dnd_chkmousedown',this.$V.heaader);
		},
		dnd_dragable:function(e){
			var src=(e.srcElement||e.target);
			if(!this.hasClassName('activated')|| this.hasClassName('iconified')||this.hasClassName('maximized'))return false;
			if(this.$V.header&&xlv.util.isDescendantOf(src,this.$V.header)){
				if(this.getStyle('position')!='absolute')return false;
				if(src.id=='xlvid_exit'||src.id=='xlvid_max'||src.id=='xlvid_min')return false;
				this.dnd_properties.sourceid='xlvid_frame_header';
				return true;
			}
			if(src.id=='xlvid_sizer') {
				this.dnd_properties.sourceid=src.id;
				return true;
			}
			return false;
		},
		dnd_dragStart:function(event){
			if(this.$P.dnd.outline){
				this.dnd_createGhost();
				this.dnd_properties.ghostFloat=false;
			}
		},
		dnd_drag:function(e){
			var src=(e.srcElement||e.target);
			var p=this.dnd_properties;
			var xy=p.offsetXY;
			var ok=true;
			if(p.sourceid=='xlvid_frame_header'){
				if(this.$P.dnd.outline){
					p.ghostElement.style.display='';
					var x=parseInt(p.ghostElement.style.left)+xy[0];
					var y=parseInt(p.ghostElement.style.top)+xy[1];
					Element.setStyle(p.ghostElement,{left:x+'px',top:y+'px'});
					ok=(y>=0);
					p.ghostFloat=(!ok);
				}else{
					xy[0]+=this.offsetLeft;
					xy[1]+=this.offsetTop;
					if(ok=(xy[1]>=0)) this.setStyle({left:xy[0]+'px',top:xy[1]+'px'});
				}
			}
			if(p.sourceid=='xlvid_sizer'){
				if(this.$P.dnd.outline){
					p.ghostElement.style.display='';
					var w=parseInt(p.ghostElement.style.width)+xy[0];
					var h=parseInt(p.ghostElement.style.height)+xy[1];
					Element.setStyle(p.ghostElement,{width:w+'px',height:h+'px'});
					ok=(w>=60&&h>=60);
				}else{
					xy[0]+=parseInt(this.style.width);
					xy[1]+=parseInt(this.style.height);
					if(ok=(xy[0]>=60&&xy[1]>=60)){
						this.setStyle({width:xy[0]+'px',height:xy[1]+'px'});
						this.layout();
					}
				}
			}
			return (ok)?'':'cancel';
		},
		dnd_createGhost:function(){
			if(!this.$P.dnd.outline)return;
			var v=this.$V;
			var p=this.dnd_properties;
			var outline=this.targetElement(this.$V.root,{id:'xlvid_glow_content'});
			outline=(outline)?outline:this;
			var lt=this.getDesktop().viewportOffset(outline);
			var div=document.createElement('div');
			var style={display:'none',position:'absolute',zIndex:xlv.sys.zIndexDnd};
			Element.setStyle(div,style);
			div.style.left=lt[0]+'px';
			div.style.top=lt[1]+'px';
			div.style.width=outline.offsetWidth+'px';
			div.style.height=outline.offsetHeight+'px';
			div.style.backgroundColor='blue';
			Element.setStyle(div,{opacity:0.05});
			p.ghostElement=div;
			xlv.desktop.appendChild(p.ghostElement);
			p.ghostHomeXY=[parseInt(p.ghostElement.style.left),parseInt(p.ghostElement.style.top)];
		},
		dnd_dragEnd:function(event){
			var v=this.$V;
			var p=this.dnd_properties;
			if(!p.dragCancel){
				if(p.sourceid=='xlvid_frame_header'){
					if(this.$P.dnd.outline){
						var x=parseInt(this.style.left)+(p._lastXY[0]-p._startXY[0]);
						var y=parseInt(this.style.top)+(p._lastXY[1]-p._startXY[1]);
						this.setStyle({left:x+'px',top:y+'px'});
					}
				}
				if(p.sourceid=='xlvid_sizer'){
					if(this.$P.dnd.outline){
						var w=parseInt(this.style.width)+(p._lastXY[0]-p._startXY[0]);
						var h=parseInt(this.style.height)+(p._lastXY[1]-p._startXY[1]);
						Element.setStyle(this,{width:w+'px',height:h+'px'});
						this.layout();
					}
				}
			}
		},
		saveStyle:function(){
    		this.$V.saveStyle={};
    		['position','top','left','right','bottom','width','height','display'].
    		each(function(k){this.$V.saveStyle[k]=this.style[k];}.bind(this));
		},
		getTitle:function(){
			var h=(this.$V.header) ? xlv.util.findChild(this.$V.header,{id:'xlvid_title'}):null;
			return (h) ? xlv.util.getElementValue(h) : '';
		},
		setTitle:function(title){
			var h=(this.$V.header) ? xlv.util.findChild(this.$V.header,{id:'xlvid_title'}):null;
			if(h) xlv.util.setElementValue(h,title);
		},
		getNote:function(){
			var f=(this.$V.footer) ? xlv.util.findChild(this.$V.footer,{id:'xlvid_note'}):null;
			return (f) ? xlv.util.getElementValue(f) : '';
		},
		setNote:function(note){
			var f=(this.$V.footer)? xlv.util.findChild(this.$V.footer,{id:'xlvid_note'}):null;
			if(f) xlv.util.setElementValue(f,note);
		}
	}
};


